Skip to content

pgconn: use fresh context for fallback connection in connectPreferred#2554

Open
c-tonneslan wants to merge 1 commit into
jackc:masterfrom
c-tonneslan:fix/connectpreferred-fallback-ctx
Open

pgconn: use fresh context for fallback connection in connectPreferred#2554
c-tonneslan wants to merge 1 commit into
jackc:masterfrom
c-tonneslan:fix/connectpreferred-fallback-ctx

Conversation

@c-tonneslan
Copy link
Copy Markdown

When ConnectTimeout is set, connectPreferred creates a per-host timeout context (derived from octx) for each host in the loop. After the loop finishes, ctx holds whatever context was used for the last host, which may have already expired by the time we get to the fallback.

The fallback path (for prefer-standby / prefer-primary) was passing that stale ctx to connectOne, so it would immediately get a deadline exceeded error even though the caller's original context still had time left.

The fix creates a fresh ConnectTimeout-bounded context from octx for the fallback, same pattern as the loop uses for each host.

Fixes #2172

When ConnectTimeout is set, connectPreferred creates a per-host timeout
context derived from octx for each host. After the loop, ctx holds
whatever context was used for the last attempt, which may have already
expired. The fallback connection (for prefer-standby/prefer-primary) was
using that stale ctx, so it would fail immediately with a deadline
exceeded error even though the original context still had time left.

Fix this by creating a fresh timeout context from octx for the fallback,
same as we do for each host in the loop.

Fixes jackc#2172
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Connection timeout wrongly applied to non-preferred hosts

1 participant